package com.low.filter;


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


/**
 * 登录检测以及菜单高亮效果呈现
 * @author D07
 *
 */
public class CheckLoginFilter implements Filter {

	private static String sessionName;
	private static  String timeOutUrl;
//	private static String[] noFilterUrl={};
	public WebApplicationContext wac;//spring容器
	public void destroy() {
		
	}
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String url = ((HttpServletRequest)request).getRequestURL().toString();
		/**
		 * ---------------------------------------登录过滤检查--------------------------------------------------
		 */
		if(((HttpServletRequest)request).getSession().getAttribute(sessionName) == null){

			((HttpServletRequest)request).getSession().setAttribute(timeOutUrl, url);
			((HttpServletResponse)response).sendRedirect(((HttpServletRequest)request).getContextPath()+timeOutUrl);
		}
		/**
		 * ---------------------------------------url动态高亮显示--------------------------------------------------
		*/
		else{
			//如果是登录请求则直接放行。
			if(url.indexOf("login")!=-1){
			}
			else{

			}
			chain.doFilter(request, response);
		}
	}
	public void init(FilterConfig arg0) throws ServletException {
		sessionName = arg0.getInitParameter("sessionName");
		timeOutUrl = arg0.getInitParameter("timeOutUrl");
		wac = WebApplicationContextUtils.getWebApplicationContext(
				arg0.getServletContext());
//		String[] bnameStrings=wac.getBeanDefinitionNames();
//		for (String string : bnameStrings) {
//			System.out.println(string);
//		}
	}

	
}
